package com.legendframework.dao.yaml.support;

import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.legendframework.dao.exception.DaoException;

import java.io.*;

/**
 * @Auther: ShallowDream7
 * @Date: 2019/7/31 22:14
 * @Description:
 */
public class BaseYml<T> extends AbstractBaseYml<T> implements IBaseYml<T> {

    private Class<T> typeClass;

    private String path;

    private Gson gson;

    /**
     * 构造器
     * @param path 文件的绝对路径前缀
     * @param typeClass 实体类类型
     */
    public BaseYml(String path , Class<T> typeClass) {
        this.path = path;
        this.typeClass = typeClass;
        //判断上级目录是否存在
        File file = new File(path).getParentFile();
        if(!file.exists()){
            //如果不存在则创建该目录
            file.mkdirs();
        }
        existsSave(null);
        gson = new GsonBuilder().serializeNulls().create();
    }

    @Override
    public synchronized void save(T t) {
        try {
            if(t != null){
                YamlWriter writer = new YamlWriter(new FileWriter(path));
                //该行代码是指写文件时不写入全限定类名作为索引
                writer.getConfig().writeConfig.setWriteRootTags(false);
                //所有类中的引用类型都不会输出全限定类名
                writer.getConfig().writeConfig.setWriteRootElementTags(false);
                //设置缩进格数
                writer.getConfig().writeConfig.setIndentSize(2);
                //写入具有默认值的对象字段
                writer.getConfig().writeConfig.setWriteDefaultValues(true);
                //设置为不抛锚点
                writer.getConfig().writeConfig.setAutoAnchor(false);
                //设置为不转义
                writer.getConfig().writeConfig.setEscapeUnicode(false);
                writer.write(t);
                writer.close();
            }else{
                //即使是空也要创建文件
                File file = new File(path);
                if(!file.exists()){
                    file.createNewFile();
                }
            }
        } catch (IOException e) {
            throw new DaoException("Yaml保存失败",e);
        }
    }

    @Override
    public T get() {
        YamlReader reader = null;
        try {
            reader = new YamlReader(new FileReader(path));
            Object read = reader.read();
            T t;
            if (typeClass != null && read != null) {
                String json = this.gson.toJson(read);
                t = this.gson.fromJson(json,typeClass);
            }else {
                t = (T) read;
            }
            reader.close();
            return t;
        } catch (Throwable e) {
            throw new DaoException("Yaml获取失败",e);
        }
    }


    @Override
    public Boolean isExist() {
        return new File(path).exists();
    }

    @Override
    public void existsSave(T t) {
        if(!isExist()){
            save(t);
        }
    }

    @Override
    public void delete() {
        File file =new File(path);
        file.delete();
    }
}
